﻿using JuCloudy.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;

namespace JuCloudy.Data.Nhibernate
{
    public class SysContainer
    {
        private static global::NHibernate.Cfg.Configuration mCfg = null;
        private static global::NHibernate.ISessionFactory SessionFactory = null;
        public SysContainer()
        {
            mCfg = new NHibernate.Cfg.Configuration().Configure(
               System.Web.HttpContext.Current.Server.MapPath(
                    System.Configuration.ConfigurationManager.AppSettings["SysConnNhCfg"]
                    ));

            SessionFactory = null;

            mCfg.AddClass(typeof(JuCloudy.Data.Entity.SysAppInfo));
            mCfg.AddClass(typeof(JuCloudy.Data.Entity.SysAppModule));
            mCfg.AddClass(typeof(JuCloudy.Data.Entity.SysCategory));
            mCfg.AddClass(typeof(JuCloudy.Data.Entity.SysStructure));
            mCfg.AddClass(typeof(JuCloudy.Data.Entity.ViewSysStructureAndCategoryAndAppInfo));
            SessionFactory = mCfg.BuildSessionFactory();
        }

        public List<SysAppInfo> DeleteAppInfo(SysAppInfo entity)
        {
            App.AppContainer<SysAppInfo> app = new App.AppContainer<SysAppInfo>("SysConnNhCfg");
            return app.Delete(entity);
        }

        public List<SysCategory> DeleteCategory(SysCategory entity)
        {
            App.AppContainer<SysCategory> app = new App.AppContainer<SysCategory>("SysConnNhCfg");
            return app.Delete(entity);
        }

        public List<SysStructure> DeleteStructure(SysStructure entity)
        {
            App.AppContainer<SysStructure> app = new App.AppContainer<SysStructure>("SysConnNhCfg");
            return app.Delete(entity);
        }

        public List<SysAppInfo> GetAppInfoEntities()
        {
            App.AppContainer<SysAppInfo> app = new App.AppContainer<SysAppInfo>("SysConnNhCfg");
            return app.GetEntities();
        }

        public SysAppInfo GetAppInfoEntity(int AppInfoId)
        {
            App.AppContainer<SysAppInfo> app = new App.AppContainer<SysAppInfo>("SysConnNhCfg");
            return app.GetEntity("AppId", AppInfoId.ToString());
        }

        public List<SysAppModule> GetAppModuleEntities(bool? IsEnable)
        {
            App.AppContainer<SysAppModule> app = new App.AppContainer<SysAppModule>("SysConnNhCfg");
            return app.GetEntities();
        }

        public SysAppModule GetAppModuleEntity(int AppModuleId)
        {
            App.AppContainer<SysAppModule> app = new App.AppContainer<SysAppModule>("SysConnNhCfg");
            return app.GetEntity("Id", AppModuleId.ToString());
        }

        public List<SysCategory> GetCategoryEntities()
        {
            App.AppContainer<SysCategory> app = new App.AppContainer<SysCategory>("SysConnNhCfg");
            return app.GetEntities();
        }

        public SysCategory GetCategoryEntity(int CategoryId)
        {
            App.AppContainer<SysCategory> app = new App.AppContainer<SysCategory>("SysConnNhCfg");
            return app.GetEntity("CategoryId",CategoryId.ToString());
        }

        public SysAppModule GetNewestAppModuleEntity(int AppId)
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                var query=from c in session.Query<JuCloudy.Data.Entity.SysAppModule>()
                          where c.AppId == AppId && c.IsEnable == true
                          select c;
                if (query != null && query.Count() > 0)
                {
                    var entity = query.OrderByDescending(p => p.CreateDateTimestamp).ToList()[0];
                    session.Flush();
                    session.Close();
                    session.Dispose();
                    return entity;
                }
                else
                {
                    session.Flush();
                    session.Close();
                    session.Dispose();
                    return null;
                }
            }
        }

        public List<SysStructure> GetStructureEntities()
        {
            App.AppContainer<SysStructure> app = new App.AppContainer<SysStructure>("SysConnNhCfg");
            return app.GetEntities();
        }

        public SysStructure GetStructureEntity(int StructureId)
        {
            App.AppContainer<SysStructure> app = new App.AppContainer<SysStructure>("SysConnNhCfg");
            return app.GetEntity("StructureId", StructureId.ToString());
        }

        public List<ViewSysStructureAndCategoryAndAppInfo> GetViewEntities()
        {
            using (ISession session = SessionFactory.OpenSession())
            {
               /*SELECT stru.StructureId, stru.StructureHeader, stru.StructureOrder, stru.IsVisable, cate.CategoryId, 
                      cate.StructureId AS CategoryStructureId, cate.CategoryName, cate.CategoryIntruduction, cate.CategoryOrder, 
                      cate.IsVisable AS CategoryVisable, app.AppId, app.CategoryId AS AppInfoCategoryId, app.AppName, 
                      app.AppIntroduction, app.IsVisable AS AppInfoVisable, app.IsEnable
FROM         dbo.SysStructure INNER JOIN
                      dbo.SysCategory ON dbo.SysStructure.StructureId = dbo.SysCategory.StructureId INNER JOIN
                      dbo.SysAppInfo ON dbo.SysCategory.CategoryId = dbo.SysAppInfo.CategoryId*/
                /*from c in customers 
3	            join o in orders on c.CustomerId equals o.CustomerId 
4	            where o.OrderId == 2 
5	            select c; */
                var query = from stru in session.Query<SysStructure>()
                            join cate in session.Query<SysCategory>() on stru.StructureId equals cate.StructureId
                            join app in session.Query<SysAppInfo>() on cate.CategoryId equals app.CategoryId
                            select new
                            {
                                stru.StructureId,
                                stru.StructureHeader,
                                stru.StructureOrder,
                                stru.IsVisable,
                                cate.CategoryId,
                                CategoryStructureId = cate.StructureId,
                                cate.CategoryName,
                                cate.CategoryIntruduction,
                                cate.CategoryOrder,
                                CategoryVisable = cate.IsVisable,
                                app.AppId,
                                AppInfoCategoryId = app.CategoryId,
                                app.AppName,
                                app.AppIntroduction,
                                AppInfoVisable = app.IsVisable,
                                app.IsEnable
                            };
                List<ViewSysStructureAndCategoryAndAppInfo> entities = new List<ViewSysStructureAndCategoryAndAppInfo>();
                foreach (var q in query)
                {
                    entities.Add(new ViewSysStructureAndCategoryAndAppInfo()
                    {
                        AppId = q.AppId,
                        AppInfoCategoryId = q.AppInfoCategoryId,
                        AppInfoVisable = q.AppInfoVisable,
                        AppIntroduction = q.AppIntroduction,
                        AppName = q.AppName,
                        CategoryId = q.CategoryId,
                        CategoryIntruduction = q.CategoryIntruduction,
                        CategoryName = q.CategoryName,
                        CategoryOrder = q.CategoryOrder,
                        CategoryStructureId = q.CategoryStructureId,
                        CategoryVisable = q.CategoryVisable,
                        IsEnable = q.IsEnable,
                        IsVisable = q.IsVisable,
                        StructureHeader = q.StructureHeader,
                        StructureId = q.StructureId,
                        StructureOrder = q.StructureId
                    });
                }
                return entities;
            }
        }

        public ViewSysStructureAndCategoryAndAppInfo GetViewEntity(int StructureId)
        {
            App.AppContainer<ViewSysStructureAndCategoryAndAppInfo> app = new App.AppContainer<ViewSysStructureAndCategoryAndAppInfo>("SysConnNhCfg");

            return app.GetEntity("StructureId", StructureId.ToString());
        }

        public List<SysAppInfo> InsertAppInfo(SysAppInfo entity)
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdate(entity);
                        vTransaction.Commit();
                        var query = session.Query<JuCloudy.Data.Entity.SysAppInfo>().ToList();
                        vTransaction.Dispose(); 
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return query;
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        throw ex;
                    }
                }
            }
        }

        public int InsertAppModule(SysAppModule entity)
        {
            /*App.AppContainer<JuCloudy.Data.Entity.SysAppModule> container =
                new App.AppContainer<SysAppModule>("SysConnNhCfg");
            container.Insert(entity);*/
            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(entity);
                        vTransaction.Commit();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return entity.Id;
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        throw ex;
                    }
                }
            }
        }

        public List<SysCategory> InsertCategory(SysCategory entity)
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdate(entity);
                        vTransaction.Commit();
                        var query = session.Query<JuCloudy.Data.Entity.SysCategory>().ToList();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return query;
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        throw ex;
                    }
                }
            }
        }

        public List<SysStructure> InsertStructure(SysStructure entity)
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdate(entity);
                        vTransaction.Commit();
                        var query = session.Query<JuCloudy.Data.Entity.SysStructure>().ToList();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return query;
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        throw ex;
                    }
                }
            }
        }
    }
}
